package com.example.hibernate.j.many2many;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;


@Entity
@Table(name="T_GATEGORY")
@SequenceGenerator(name="SEQ_GATEGORY", sequenceName="SEQ_GATEGORY", initialValue=1)
public class Category {
	
	@Id
	@GeneratedValue(generator="SEQ_GATEGORY")
	private Long id;
	private String name;
	
	@ManyToMany(cascade={CascadeType.ALL}, targetEntity=com.example.hibernate.j.many2many.Item.class)
	@JoinTable(
			name="CATEGORY_ITEM",
			joinColumns={@JoinColumn(name="CATEGORY_ID")},
			inverseJoinColumns={@JoinColumn(name="ITEM_ID")}
		)
	private Set<Item> items = new HashSet<Item>();
	
	/*
	>>>>>>>>>>>>>>>>>>>>>>>> List
	@ManyToMany
	@JoinTable(
			name="CATEGORY_ITEM",
			joinColumns={@JoinColumn(name="CATEGORY_ID")},
			inverseJoinColumns={@JoinColumn(name="ITEM_ID")}
		)
	@IndexColumn(name="CATEGORY_POSITION")
	private List<Item> itemsList = new ArrayList<Item>();
	
	>>>>>>>>>>>>>>>>>>>> Bag
	@ManyToMany
	@JoinTable(
			name="CATEGORY_ITEM",
			joinColumns={@JoinColumn(name="CATEGORY_ID")},
			inverseJoinColumns={@JoinColumn(name="ITEM_ID")}
		)
	@CollectionId(
			columns=@Column(name="CATEGORY_ITEM_ID"),
			type=@Type(type="long"),
			generator="sequence"
		)
	private Collection<Item> itemsIdBag = new ArrayList<Item>();
	*/
	
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Item> getItems() {
		return items;
	}

	public void setItems(Set<Item> items) {
		this.items = items;
	}

	@Override
	public String toString() {
		return "Category [id=" + id + ", name=" + name + "]";
	}
	
}
